package com.lyf.shop.web.controller;

import com.lyf.shop.common.Const;
import com.lyf.shop.entity.Address;
import com.lyf.shop.entity.User;
import com.lyf.shop.services.AddressService;
import com.lyf.shop.services.Impl.AddressServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import sun.awt.ConstrainableGraphics;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/addr")
public class addressController extends BaseController{
    private AddressService addressService = new AddressServiceImpl();
    public String showAddr(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute(Const.UserConst.LOGIN_USER);
        if(user==null){
            return Const.REDIRECT+"/login.jsp";
        }
        //业务层
        List<Address> addressList = addressService.getAddressByUid(user.getUid());
        request.setAttribute("addList",addressList);
        return Const.FORWARD+"/self_info.jsp";
    }
    public String insertAddr(HttpServletRequest request, HttpServletResponse response){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Address a = new Address();
        try {
            BeanUtils.populate(a,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //新增的地址设为非默认
        a.setAstate(0);
        //业务层
        addressService.insertAddr(a);
        return Const.FORWARD+"/addr?method=showAddr";
    }
    public String delete(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute(Const.UserConst.LOGIN_USER);
        if(user==null){
            return Const.REDIRECT+"/login.jsp";
        }
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        addressService.delAddr(aid);
        return Const.FORWARD+"/addr?method=showAddr";
    }
    public String updateAddr(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute(Const.UserConst.LOGIN_USER);
        if(user==null){
            return Const.REDIRECT+"/login.jsp";
        }
        Map<String, String[]> parameterMap = request.getParameterMap();
        Address a = new Address();
        try {
            BeanUtils.populate(a,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        addressService.updateAddr(a);
        return Const.FORWARD+"/addr?method=showAddr";
    }

    public String changeDefault(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute(Const.UserConst.LOGIN_USER);
        if(user==null){
            return Const.REDIRECT+"/login.jsp";
        }
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        addressService.changeAddrToCommons(aid,user.getUid());
        return Const.FORWARD+"/addr?method=showAddr";
    }
}
